import requests
from selenium import webdriver
import time
import re
import asyncio
import configparser
from DbUtil import Dbutil
def getSkuInfo(sku_code):
    pro = webdriver.Chrome(executable_path="D:\迅雷下载\chromedriver_win32\chromedriver.exe")
    pro.get('https://item.jd.com/'+ sku_code +'.html')
    dic = {}
    # 获取价格
    price = pro.find_element_by_xpath('//div[@class="summary-price-wrap"]/div[1]/div[2]/span[1]/span[2]').text
    pro.quit()
    return price

def getGoodInfo(url, thumbnail):
    pro = webdriver.Chrome(executable_path="D:\迅雷下载\chromedriver_win32\chromedriver.exe")
    pro.get(url)

    goodInfo = {}
    goodInfo['url'] = url
    goodInfo['thumbnail'] = thumbnail

    # 获取轮播图
    img_ele_list = pro.find_elements_by_xpath('//*[@id="spec-list"]/ul/li/img')
    banner_list = []
    for img_ele in img_ele_list:
        banner_list.append('https:////img11.360buyimg.com/n1/' + img_ele.get_attribute('data-url'))
    goodInfo['banners'] = banner_list
    # 获取商品名称
    goodInfo['name'] = pro.find_element_by_xpath('//div[@class="sku-name"]').text

    # 获取详情图片
    good_pics_ele_list = pro.find_elements_by_xpath('//*[@id="J-detail-content"]/div[@class="ssd-module-wrap"]/div')
    detail_img_list = []
    for good_pic_ele in good_pics_ele_list:
        pic_url_attr = good_pic_ele.value_of_css_property('background-image')
        pic_url = re.match('url\(\"(.*)\"', pic_url_attr, re.M|re.I)
        if pic_url != None:
            detail_img_list.append(pic_url.group(1))

    goodInfo['details'] = detail_img_list

    # 获取规格
    spec = []
    good_spec_ele_list = pro.find_elements_by_xpath('//*[@id="choose-attr-1"]/div[2]/div')
    for good_spec_ele in good_spec_ele_list:
        sku_code = good_spec_ele.get_attribute('data-sku')
        sku_name = good_spec_ele.find_element_by_xpath('./a/i').text
        sku_img = good_spec_ele.find_element_by_xpath('./a/img').get_attribute('src')
        try:
            sku_price = getSkuInfo(sku_code)
            sku_info = {}
            sku_info['code'] = sku_code
            sku_info['name'] = sku_name
            sku_info['icon'] = sku_img
            sku_info['price'] = sku_price
            spec.append(sku_info)
        except:
            continue
        finally:
            pass

    goodInfo['spec'] = spec
    print('goodInfo:' + str(goodInfo))
    pro.quit()
    return goodInfo

def getGoodList(pro):
    i = 0
    while i < 100:
        i = i + 1
        if config.get('jd', 'state') == 'stop':  #  如果配置文件中修改成stop 就停止抓取
            break

        page = config.get('jd', 'page')
        page = int(page) + 1
        pro.get("https://search.jd.com/Search?keyword=化妆品&page=" + str(page))
        good_ele_list = pro.find_elements_by_xpath('//*[@id="J_goodsList"]/ul/li')

        pro.execute_script("window.scrollTo(0,window.screen.height * 1 )")
        time.sleep(1)
        pro.execute_script("window.scrollTo(0,window.screen.height * 2 )")
        time.sleep(1)
        pro.execute_script("window.scrollTo(0,window.screen.height * 3 )")
        time.sleep(1)
        pro.execute_script("window.scrollTo(0,window.screen.height * 4 )")
        time.sleep(2)

        good_list = []
        for good_ele in good_ele_list:
            good_a = good_ele.find_element_by_xpath('./div/div/a')
            good_url = good_a.get_attribute('href')
            good_img = good_ele.find_element_by_xpath('./div/div/a/img')
            good_thumbnail = good_img.get_attribute('src')
            good_price_ele = good_ele.find_element_by_xpath('./div/div[@class="p-price"]/strong/i')
            good_price = good_price_ele.text
            dic = {}
            dic['url'] = good_url
            dic['thumbnail'] = good_thumbnail
            dic['price'] = good_price
            good_list.append(dic)

        for dic in good_list:
            # print('goodUrl:' + dic['url'])
            # getGoodInfo(dic['url'], dic['thumbnail'])  # 先抓取所有商品网址， 先不抓取详情
            db.insertGoodUrl('化妆品', dic['url'], dic['thumbnail'], dic['price'])

        config.set('jd', 'page', str(page))
        config.write(open("config.ini", "w"))
        print(good_list)
        # break

def getGoodListInfo():
    db = Dbutil()
    while 1:
        goodUrl = Dbutil().getOneGoodUrl()
        goodInfo = getGoodInfo(url=goodUrl.goodurl, thumbnail=goodUrl.thumbnail)

        sku = goodUrl.goodurl.split('/')[-1].split('.')[0]
        db.insertGoodInfo(name=goodInfo['name'], price=goodUrl.price , description=str(goodInfo['details']), spu=sku, spec=str(goodInfo['spec']), banner=str(goodInfo['banners']), thumbnail=goodInfo['thumbnail'])
        db.updateGoodUrl(goodUrl.id)

config = configparser.ConfigParser()
db = Dbutil()
if __name__ == '__main__':
    config.read("config.ini", encoding="utf-8")

    #获取京东列表中所有的商品信息
    # pro = webdriver.Chrome(executable_path="D:\迅雷下载\chromedriver_win32\chromedriver.exe")
    # getGoodList(pro)
    # pro.quit()

    # getGoodInfo('https://item.jd.com/2326961.html', 'aaaa')

    getGoodListInfo()